import { http } from "./index";
import { Message } from "element-ui";
//登录
export const login = async (data) => {
  try {
    const res = await http("/login", "POST", data);
    Message.success(res.meta.msg);
    return res;
  } catch (error) {
    throw new Error(error);
    // Promise.reject(error)
  }
};
//获取左侧权限菜单
export const getMenus = async () => {
  try {
    const res = await http("/menus");
    return res.data;
  } catch (error) {
    throw new Error(error);
  }
};
//获取用户列表
export const getUsers = async (params) => {
  try {
    const res = await http("/users", "GET", {}, params);
    return res.data;
  } catch (error) {
    throw new Error(error);
  }
};
//改变用户状态可用||不可用
//users/:uId/state/:type
export const changeUserState = async ({ uId, type }) => {
  try {
    const res = await http(`/users/${uId}/state/${type}`, "PUT");
    Message.success(res.meta.msg);
    return res;
  } catch (error) {
    throw new Error(error);
  }
};
//添加用户
export const addUser = async (data) => {
  try {
    const res = await http("/users", "POST", data);
    Message.success(res.meta.msg);
    return res;
  } catch (error) {
    throw new Error(error);
  }
};

//删除用户

export const delUser = async (id) => {
  try {
    const res = await http(`/users/${id}`, "DELETE");
    Message.success(res.meta.msg);
    return res;
  } catch (error) {
    throw new Error(error);
  }
};
//编辑用户
export const editUser = async (data) => {
  try {
    const res = await http(`/users/${data.id}`, "PUT", {
      ...data,
    });
    Message.success(res.meta.msg);
    return res;
  } catch (error) {
    throw new Error(error);
  }
};

//根据用户id查询角色id
export const getRoleId = async (id) => {
  try {
    const res = await http(`/users/${id}`);
    return res.data;
  } catch (error) {
    throw new Error(error);
  }
};
//获取角色列表
export const getRoles = async () => {
  try {
    const res = await http(`/roles`);
    return res.data;
  } catch (error) {
    throw new Error(error);
  }
};

//分配角色

export const distUserRole = async (data) => {
  try {
    const res = await http(`/users/${data.id}/role`, "PUT", data);
    Message.success(res.meta.msg);
    return res;
  } catch (error) {
    throw new Error(error);
  }
};

//添加角色
export const addRole = async (data) => {
  try {
    const res = await http("/roles", "POST", data);
    Message.success(res.meta.msg);
    return res;
  } catch (error) {
    throw new Error(error);
  }
};
//删除角色
export const deleteRole = async (id) => {
  try {
    const res = await http("/roles/" + id, "DELETE");
    Message.success(res.meta.msg);
    return res;
  } catch (error) {
    throw new Error(error);
  }
};
//更新角色
export const updateRole = async (data) => {
  try {
    const res = await http("/roles/" + data.id, "PUT", data);
    Message.success("更新角色信息成功");
    return res;
  } catch (error) {
    throw new Error(error);
  }
};

//删除角色对应权限

export const deleteRightsByRole = async ({ roleId, rightId }) => {
  try {
    const res = await http(`roles/${roleId}/rights/${rightId}`, "DELETE");
    Message.success(res.meta.msg);
    return res;
  } catch (error) {
    throw new Error(error);
  }
};
//获取权限列表tree

export const getRights = async () => {
  try {
    const res = await http(`/rights/tree`);
    return res.data;
  } catch (error) {
    throw new Error(error);
  }
};

//角色授权

export const giveRoleRights = async ({ id, rids }) => {
  try {
    const res = await http(`/roles/${id}/rights`, "POST", { rids });
    Message.success(res.meta.msg);
    return res;
  } catch (error) {
    throw new Error(error);
  }
};

//获取商品列表
export const getGoodsLists = async ({ query = "", pagenum, pagesize }) => {
  try {
    const res = await http(
      `/goods?query=${query}&pagenum=${pagenum}&pagesize=${pagesize}`
    );
    return res.data;
  } catch (error) {
    throw new Error(error);
  }
};

//删除商品

export const delGoods = async (id) => {
  try {
    const res = await http(`/goods/${id}`, "DELETE");
    Message.success(res.meta.msg);
    return res;
  } catch (error) {
    throw new Error(error);
  }
};

//获取三级分类列表
export const getCategories = async () => {
  try {
    const res = await http(`/categories?type=3`);
    return res.data;
  } catch (error) {
    throw new Error(error);
  }
};

//添加商品
export const addGoods = async (data) => {
  try {
    const res = await http(`/goods`, "POST", data);
    Message.success(res.meta.msg);
    return res.data;
  } catch (error) {
    throw new Error(error);
  }
};

//获取商品参数
export const getAttrs = async ({ id, sel }) => {
  try {
    const res = await http(`/categories/${id}/attributes?sel=${sel}`);
    return res.data;
  } catch (error) {
    throw new Error(error);
  }
};
//获取商品详情

export const getGoodsDetail = async (id) => {
  try {
    const res = await http(`/goods/${id}`);
    return res.data;
  } catch (error) {
    throw new Error(error);
  }
};

//编辑商品
// goods/:id
export const editGoods = async (data) => {
  try {
    const res = await http(`/goods/${data.goods_id}`, "PUT", data);
    Message.success(res.meta.msg);
    return res;
  } catch (error) {
    throw new Error(error);
  }
};

//添加动态或者静态参数

export const addParams = async (data) => {
  try {
    const res = await http(`/categories/${data.id}/attributes`, "POST", data);
    Message.success(res.meta.msg);
    return res.data;
  } catch (error) {
    throw new Error(error);
  }
};

//编辑动态或者静态参数

export const editParams = async (data) => {
  try {
    const res = await http(
      `/categories/${data.id}/attributes/${data.attr_id}`,
      "PUT",
      data
    );
    Message.success(res.meta.msg);
    return res;
  } catch (error) {
    throw new Error(error);
  }
};

//获取图表数据
export const getReport = async (type) => {
  try {
    const res = await http(`/reports/type/${type}`);
    return res.data;
  } catch (error) {
    throw new Error(error);
  }
};
